// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-10-7

#include "stdafx.h"
#include ".\Mytrayicon.h"
#include "resource.h"
#include "CoolPopupMenu.h"

#include <common\ui\manager.h>
#include "mainuiman.h"



class CCoolPopupMenu;

int AFXAPI AfxLoadString(UINT nID, LPSTR lpszBuf, UINT nMaxBuf);

IMPLEMENT_DYNAMIC(CMyTrayIcon, CCmdTarget) 


//##ModelId=44C4B5E002D3
CMyTrayIcon::CMyTrayIcon(UINT uID,CCoolPopupMenu *m_Menu)
{
    // Initialize NOTIFYICONDATA 
    memset(&m_nid, 0 , sizeof(m_nid)); 
    m_nid.cbSize = sizeof(m_nid);
    m_nid.uID = uID; // never changes after construction 

    // Use resource string as tip if there is one 
    AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip)); 
	m_mnu=m_Menu;   

}

//##ModelId=44C4B5E002D6
CMyTrayIcon::~CMyTrayIcon(void)
{
    // remove icon from system tray 
    SetIcon(0);
}

//##ModelId=44C4B5E002EE
void CMyTrayIcon::SetNotificationWnd(CWnd* pNotifyWnd, UINT uCbMsg) 
{ 
    ASSERT(pNotifyWnd != NULL && ::IsWindow(pNotifyWnd->GetSafeHwnd())); 
    ASSERT(uCbMsg ==0 || uCbMsg>=WM_USER); 

    m_nid.hWnd = pNotifyWnd->GetSafeHwnd(); 
    m_nid.uCallbackMessage = uCbMsg; 
} 

//##ModelId=44C4B5E002F1
BOOL CMyTrayIcon::SetIcon(UINT uID)
{ 
    HICON hicon=NULL; 
    if (uID)
    {
        AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip)); 
        hicon = AfxGetApp()->LoadIcon(uID); 
    } 
    return SetIcon(hicon, NULL); 
} 

//##ModelId=44C4B5E002FD
BOOL CMyTrayIcon::SetIcon(HICON hicon, LPCSTR lpTip) 
{ 
    UINT msg;
    m_nid.uFlags = 0;

    // Set the icon
    if (hicon)
    {
        // Add or replace icon in system tray 
        //msg = m_nid.hIcon ? NIM_MODIFY : NIM_ADD; 
	    msg = m_nid.hIcon ? NIM_DELETE : NIM_ADD; 
        m_nid.hIcon = hicon; 
        m_nid.uFlags |= NIF_ICON; 
    }
    else
    {
        // remove icon from tray 
        if (m_nid.hIcon == NULL) 
        {
            return TRUE; // already deleted 
        }
        msg = NIM_DELETE; 
    } 

    // Use the tip, if any 
    if (lpTip) 
    {
        strncpy(m_nid.szTip, lpTip, sizeof(m_nid.szTip)); 
    }

    if (m_nid.szTip[0]) 
    {
        m_nid.uFlags |= NIF_TIP; 
    }

    // Use callback if any 
    if (m_nid.uCallbackMessage && m_nid.hWnd) 
    {
        m_nid.uFlags |= NIF_MESSAGE; 
    }

    // Do it 
    BOOL bRet = Shell_NotifyIcon(msg, &m_nid); 

    if (msg == NIM_DELETE || !bRet) 
    {
        m_nid.hIcon = NULL; // failed 
    }

    return bRet; 

} 

//##ModelId=44C4B5E002DF
LRESULT CMyTrayIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent,bool windowstatus) 

{ 
    if (wID!=m_nid.uID || 
        (lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK)) 
    {
        return 0; 
    }

  
	CMenu* pSubMenu = m_mnu->GetSubMenu(0);
	pSubMenu=(CMenu*)m_mnu;
   if (!pSubMenu) 
    {
        return 0; 
    }
	
    if (lEvent == WM_RBUTTONUP)
    { 
		
        CPoint mouse; 
        GetCursorPos(&mouse); 
		  ::SetForegroundWindow(m_nid.hWnd); 
		 /* ::TrackPopupMenu(m_mnu->m_hMenu,
            0, mouse.x, mouse.y, 0, m_nid.hWnd, NULL);*/
		  CJoyMainWnd* pMainWnd = ((CJoyMainManager*)JoyGetManager())->GetMainWnd();
        	
	       m_mnu->TrackPopupMenu(TPM_LEFTALIGN,mouse.x,mouse.y,(CWnd *)pMainWnd);
    
    }
    else
    {
        // double click: execute three menu item 
		if(!windowstatus)
		{
			::SendMessage(m_nid.hWnd,
            WM_COMMAND, 1002, 0);
		}else
		{
			::SendMessage(m_nid.hWnd,
            WM_COMMAND, 1003, 0);
		}
    }
 
    return 1; // handled 
} 
